home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / SLAX 6.0.8 / slax-6.0.8.iso / slax / base / 006-devel.lzm / usr / include / kio / connection.h < prev    next >
Encoding:
C/C++ Source or Header  |  2005-10-10  |  3.9 KB  |  159 lines

  1. // -*- c++ -*-
  2. /* This file is part of the KDE libraries
  3.     Copyright (C) 2000 Stephan Kulow <coolo@kde.org>
  4.                        David Faure <faure@kde.org>
  5.  
  6.     This library is free software; you can redistribute it and/or
  7.     modify it under the terms of the GNU Library General Public
  8.     License as published by the Free Software Foundation; either
  9.     version 2 of the License, or (at your option) any later version.
  10.  
  11.     This library is distributed in the hope that it will be useful,
  12.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14.     Library General Public License for more details.
  15.  
  16.     You should have received a copy of the GNU Library General Public License
  17.     along with this library; see the file COPYING.LIB.  If not, write to
  18.     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  19.     Boston, MA 02110-1301, USA.
  20. */
  21.  
  22. #ifndef __connection_h__
  23. #define __connection_h__
  24.  
  25. #include <kdelibs_export.h>
  26.  
  27. #include <sys/types.h>
  28.  
  29. #include <stdio.h>
  30. #include <qptrlist.h>
  31. #include <qobject.h>
  32.  
  33. class KSocket;
  34. class QSocketNotifier;
  35.  
  36. namespace KIO {
  37.  
  38.     struct KIO_EXPORT Task {
  39.     int cmd;
  40.     QByteArray data;
  41.     };
  42.  
  43.     /**
  44.      * This class provides a simple means for IPC between two applications
  45.      * via a pipe.
  46.      * It handles a queue of commands to be sent which makes it possible to
  47.      * queue data before an actual connection has been established.
  48.      */
  49.     class KIO_EXPORT Connection : public QObject
  50.     {
  51.     Q_OBJECT
  52.     public:
  53.     /**
  54.      * Creates a new connection.
  55.      * @see init()
  56.      */
  57.     Connection();
  58.     virtual ~Connection();
  59.  
  60.         /**
  61.      * Initialize this connection to use the given socket.
  62.      * @param sock the socket to use
  63.      * @see inited()
  64.      */
  65.     void init(KSocket *sock);
  66.         /**
  67.      * Initialize the connection to use the given file
  68.      * descriptors.
  69.      * @param fd_in the input file descriptor to use
  70.      * @param fd_out the output file descriptor to use
  71.      * @see inited()
  72.      */
  73.     void init(int fd_in, int fd_out); // Used by KDENOX
  74.     void connect(QObject *receiver = 0, const char *member = 0);
  75.         /// Closes the connection.
  76.     void close();
  77.     
  78.         /**
  79.      * Returns the input file descriptor.
  80.      * @return the input file descriptor
  81.      */
  82.     int fd_from() const { return fd_in; }
  83.         /**
  84.      * Returns the output file descriptor.
  85.      * @return the output file descriptor
  86.      */
  87.         int fd_to() const { return fileno( f_out ); }
  88.  
  89.         /**
  90.      * Checks whether the connection has been initialized.
  91.      * @return true if the initialized
  92.      * @see init()
  93.      */
  94.     bool inited() const { return (fd_in != -1) && (f_out != 0); }
  95.     
  96.         /** 
  97.      * Sends/queues the given command to be sent.
  98.      * @param cmd the command to set
  99.      * @param arr the bytes to send
  100.      */
  101.     void send(int cmd, const QByteArray &arr = QByteArray());
  102.  
  103.         /** 
  104.      * Sends the given command immediately.
  105.      * @param _cmd the command to set
  106.      * @param data the bytes to send
  107.      * @return true if successful, false otherwise
  108.      */
  109.     bool sendnow( int _cmd, const QByteArray &data );
  110.  
  111.     /**
  112.      * Receive data.
  113.      *
  114.      * @param _cmd the received command will be written here
  115.      * @param data the received data will be written here
  116.      * @return >=0 indicates the received data size upon success
  117.      *         -1  indicates error
  118.      */
  119.     int read( int* _cmd, QByteArray &data );
  120.  
  121.         /**
  122.          * Don't handle incoming data until resumed.
  123.          */
  124.         void suspend();
  125.  
  126.         /**
  127.          * Resume handling of incoming data.
  128.          */
  129.         void resume();
  130.  
  131.         /**
  132.          * Returns status of connection.
  133.      * @return true if suspended, false otherwise
  134.          */
  135.         bool suspended() const { return m_suspended; }
  136.  
  137.     protected slots:
  138.         void dequeue();
  139.     
  140.     protected:
  141.     
  142.     
  143.     private:
  144.     int fd_in;
  145.     FILE *f_out;
  146.     KSocket *socket;
  147.     QSocketNotifier *notifier;
  148.     QObject *receiver;
  149.     const char *member;
  150.     QPtrList<Task> tasks;
  151.         bool m_suspended;
  152.     private:
  153.     class ConnectionPrivate* d;
  154.     };
  155.  
  156. }
  157.  
  158. #endif
  159.